package com.nulp.psycho.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.nulp.psycho.db.ConnectionManager;
import com.nulp.psycho.db.dao.BaseDao;
import com.nulp.psycho.db.mapping.Converter;
import com.nulp.psycho.db.model.Test;

public class TestDao implements BaseDao{

	private final String ADD_TEST = "INSERT INTO test (name, description, url) VALUES (?,?,?)";
	private final String GET_TEST_BY_ID = "SELECT * FROM test WHERE id = ?";
	private final String REMOVE_TEST = "DELETE FROM test WHERE id = ?";
	private final String UPDATE_TEST = "UPDATE test SET name=?, description=?, url=? WHERE id = ?";
	
	private final String GET_ALL_TESTS = "SELECT * FROM test";
	
	@Override
	public <T> void create(T element) {
		Test temp = (Test) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_TEST)) {
			preparedStatement.setString(1, temp.getName());
			preparedStatement.setString(2, temp.getDescription());
			preparedStatement.setString(3, temp.getUrl());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		Test test = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_TEST_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				test = Converter.<Test> fromResaultSetToObject(rs,
						Test.class);
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) test;		
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_TEST)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {
		Test temp = (Test) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_TEST)) {
			preparedStatement.setInt(4, temp.getId());
			preparedStatement.setString(1, temp.getName());
			preparedStatement.setString(2, temp.getDescription());
			preparedStatement.setString(3, temp.getUrl());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getAllTests() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Test> tests = new ArrayList<Test>();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALL_TESTS)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				tests.add(Converter.<Test> fromResaultSetToObject(rs, Test.class));
			}
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) tests;
	}
	
}
